package nimo.elibrary.actions;

import com.google.gson.Gson;
import nimo.elibrary.Url;
import nimo.elibrary.WebService;
import nimo.elibrary.json.model.book.getInfoResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Enkuso
 * Date: 12/14/12
 * Time: 1:16 PM
 * To change this template use File | Settings | File Templates.
 */
public class Book {

    public static nimo.elibrary.json.model.book.Book getBookDetails(String bookId, String token)
    {

        // GET-р илгээх өгөгдлүүдийг оноох
        List<NameValuePair> nameValuePairs;
        if(token == null) {
            nameValuePairs = new ArrayList<NameValuePair>(0);
        }
        else {
            nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("token", token));
        }

        InputStream source = WebService.retrieveStream(Url.apiBaseUrl + "/books/getInfo/" + bookId + "." + Url.responseType, nameValuePairs);

        if(source != null){
            Gson gson = new Gson();

            Reader reader = new InputStreamReader(source);

            getInfoResponse response = gson.fromJson(reader, getInfoResponse.class);

            if(response.stat.equals(WebService.STATUS_OK)){
                nimo.elibrary.json.model.book.Book book = response.book;

                return book;
            }
            else {
                // Book not found
                return null;
            }
        }
        else {
            // Сэрвэртэй холбогдож чадаагүй
            return null;
        }
    }
}
